<?php

namespace app\index\controller;

use app\admin\model\CategoryModule;
use app\admin\model\IndexBlock;
use app\admin\model\Slider;
use think\Controller;
use think\Db;
use think\captcha\Captcha;

class Index extends Base
{
    protected $beforeActionList=['category'];

    public function index()
    {
        //轮播图
        $slider = Slider::where(['status'=>1])->order('sort asc,id asc')->select()->visible(['thumb','link'])->toArray();
        //关于
        $block1 = IndexBlock::index('3');
        //锚头
        $block2 = IndexBlock::index('4');
        //课程特色
        $block3 = IndexBlock::index('5');
        //教学模式----------------
        $block4 = IndexBlock::index('6');
        //研学技术服务-----
        $block5 = IndexBlock::index('7');
        //丰富的运营经验
        $block6 = IndexBlock::index('8');
        //可靠的安全保障
        $block7 = IndexBlock::index('9');
        //推荐课程
        $block8 = IndexBlock::index('2');
        //博大新闻
        $block9 = IndexBlock::index('1');

        //产品服务
        $aryProductService = array();
        $aryProductService = CategoryModule::where( [ 'pid' => 3 , 'status' => 1 , 'is_deleted' => 1 ] )->order( 'sort asc,id asc' )->select();

            $this->assign([
            'slider'=>$slider,
            'block1'=>$block1,
            'block2'=>$block2,
            'block3'=>$block3,
            'block4'=>$block4,
            'block5'=>$block5,
            'block6'=>$block6,
            'block7'=>$block7,
            'block8'=>$block8,
            'block9'=>$block9,
            'aryProductService'=>$aryProductService,
        ]);
        return view();
    }

    public function verify()
    {   ob_clean();
        $captcha = new Captcha();
        return $captcha->entry();
    }

    public function apply(){
        if ($this->request->isPost()){
            $post = $this->request->post('','','htmlspecialchars');
            $arr = ['name','tel','code'];
            $arrCode = array("name"=>"用户名","tel"=>"手机号码","code"=>"验证码");
            $captcha = new Captcha();
            foreach ($arr as $v){
                if (empty($post[$v])){
                    return ['code'=>$arrCode[$v]."不能为空"];
                }
                if ($v=='tel' && !preg_phone($post[$v])){
                    return ['code'=>$arrCode[$v]."格式错误"];
                }
                if($v=='code' && !$captcha->check($post[$v])){
                    return ['code'=>$arrCode['code']."错误"];
                }
            }

            unset($post['code']);
            unset($post['dosubmit']);
            $post['mail'] = "";
            $post['content'] = "";
                Db::name('msg')->insert([
                    'create_at'=>date('Y-m-d H:i:s'),
                    'content'=>json_encode($post,320),
                    'type'=>1,
                    'lesson_id'=>""
                ]);
                return ['code'=>"恭喜您，提交成功！我们将第一时间联系您！"];

        }
    }

}
